// components/weekCalendar/weekCalendar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    fixed: null,
  },

  /**
   * 组件的初始数据
   */
  data: {
    weekDays: [],//周期模板
    // selWeekDay: {},//当前选中的日期
    // currentMonth: 6,//当前月份
  },

  ready: function () {
    var that = this;
    that.weekInit();
  },

  /**
   * 组件的方法列表
   */
  methods: {
    weekInit: function () {
      var that = this;
      var weekDays = [], selWeekDay;
      for (var i = 0; i < 7; i++) {
        var thatTime = that.time(i);
        var temp = {
          day: thatTime.day,
          weekText: thatTime.weekText,
          time: thatTime.typeB,
        };
        if (i === 0) selWeekDay = temp;
        weekDays.push(temp);
      }

      that.triggerEvent('selectDay', selWeekDay);

      that.setData({
        weekDays: weekDays,
        selWeekDay: selWeekDay,
        currentMonth: Number(that.time().month),
      })
    },
    /**
     * 选择时间
     */
    changeTime: function (event) {
      var that = this;
      var index = event.currentTarget.dataset.index;
      let weekDays = that.data.weekDays;
      let selWeekDay = weekDays[index];

      that.triggerEvent('selectDay', selWeekDay);

      that.setData({
        weekDays: weekDays,
        selWeekDay: selWeekDay,
      })
    },
    /**
     * 获取年月日
     * params n 与当天时间的差值
     */
    time: function (n = 0) {
      var that = this;
      let showMonth = that.data.showMonth;
      let date = new Date();

      date.setDate(date.getDate() + n);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let week = date.getDay();
      let msec = date.getTime();
      if (month < 10) {
        month = '0' + month;
      }
      if (day < 10) {
        day = '0' + day;
      }

      let weekText;
      switch (week) {
        case 1:
          weekText = "一";
          break;
        case 2:
          weekText = "二";
          break;
        case 3:
          weekText = "三";
          break;
        case 4:
          weekText = "四";
          break;
        case 5:
          weekText = "五";
          break;
        case 6:
          weekText = "六";
          break;
        case 0:
          weekText = "日";
          break;
      }

      return {
        typeA: month + '月' + day + '日',
        typeB: year + '-' + month + '-' + day,
        year: year,
        month: month,
        day: day,
        week: week,
        weekText: weekText,
        msec: msec, //毫秒
        dayMsec: 1000 * 60 * 60 * 24, //一天的毫秒
      };
    },
  },
})